package com.kpmg.datalake.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

import java.util.Locale;

/**
 * 获取国际化 通用方法
 *
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-06-28 10:56:00
 */
public class MessageSourceHelperUtil implements ApplicationContextAware {
    /**
     * context 上下文
     */
    private static ApplicationContext ctx;
    /**
     * 获取国际化配置处理信息
     */
    private static MessageSource messageSource;

    public MessageSourceHelperUtil() {
    }

    public MessageSourceHelperUtil(MessageSource messageSource) {
        if (MessageSourceHelperUtil.messageSource == null) {
            MessageSourceHelperUtil.messageSource = messageSource;
        }
    }

    /**
     * 获取国际化
     *
     * @param code           国际化编码
     * @param args           参数
     * @param defaultMessage 默认值
     * @param locale         语言
     * @return 国际化信息
     */
    public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        if (messageSource == null) {
            messageSource = ctx.getBean(MessageSource.class);
        }
        String msg = messageSource.getMessage(code, args, defaultMessage, locale);
        return StringUtils.isNotBlank(msg) ? msg.trim() : msg;
    }

    /**
     * 获取国际化
     *
     * @param code   国际化编码
     * @param locale 语言
     * @return 国际化信息
     */
    public static String getMessage(String code, Locale locale) {
        return getMessage(code, null, null, locale);

    }


    public static String getMessage(String code) {
        return getMessage(code, ContextHolderUtils.getLocale());

    }

    /**
     * Set the ApplicationContext that this object runs in.
     * Normally this call will be used to initialize the object.
     * <p>Invoked after population of normal bean properties but before an init callback such
     * as {@link InitializingBean#afterPropertiesSet()}
     * or a custom init-method. Invoked after {@link org.springframework.context.ResourceLoaderAware#setResourceLoader},
     * {@link org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher} and
     * {@link org.springframework.context.MessageSourceAware}, if applicable.
     *
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws org.springframework.context.ApplicationContextException in case of context initialization errors
     * @throws BeansException                                          if thrown by application context methods
     * @see BeanInitializationException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        if (ctx == null) {
            ctx = applicationContext;
        }
    }
}
